//
//  AddContactsTableViewCell.m
//  Findif
//
//  Created by Mac on 2019/2/18.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "AddContactsTableViewCell.h"

@interface AddContactsTableViewCell ()

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, weak)UIView * lineView;

@end

@implementation AddContactsTableViewCell


- (void)setStatus:(NSString *)status{
    
    _status = status;
    
    if ([status isEqualToString:@"0"]) {
        
        self.billSwitch.on = NO;
    }else{
        
        self.billSwitch.on = YES;
    }
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = ColorWithRGB(0xF4F5F6);
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor  =ZRWhiteColor;
    backView.layer.cornerRadius = 5 * SCREENH6;
    backView.layer.masksToBounds = YES;
    
    self.backView = backView;
    
    [self.contentView addSubview:self.backView];
    
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.textColor = ZRBlackColor;
    titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.titleLabel = titleLabel;
    
    [self.backView addSubview:self.titleLabel];
    
    
    UILabel * subLabel = [UILabel new];
    
    subLabel.textColor = ColorWithRGB(0x909090);
    subLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    subLabel.text = @"不限";
    
    self.subLabel = subLabel;
    
    [self.backView addSubview:self.subLabel];
    
    
    UIImageView * rightImageView = [UIImageView new];
    
    rightImageView.image = [UIImage imageNamed:@"右箭头"];
    
    self.rightImageView = rightImageView;
    
    [self.backView addSubview:self.rightImageView];
    
    
    UISwitch * billSwitch = [UISwitch new];
    
    billSwitch.on = NO;
    [billSwitch setTintColor:ColorWithRGB(0xeeeeee)];
    [billSwitch setOnTintColor:ColorWithRGB(0xFFE055)];
    [billSwitch setThumbTintColor:ZRWhiteColor];
    billSwitch.backgroundColor = ColorWithRGB(0xeeeeee);
    billSwitch.layer.cornerRadius = 15.5 * SCREENH6;
    billSwitch.layer.masksToBounds = YES;
    [billSwitch addTarget:self action:@selector(switchAction) forControlEvents:UIControlEventValueChanged];
    self.billSwitch = billSwitch;
    
    [self.contentView addSubview:self.billSwitch];
    
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xe4e4e4);
    
    self.lineView = lineView;
    
    [self.backView addSubview:self.lineView];
}

- (void)layoutSubviews{
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(self.contentView);
        make.leading.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-15 * SCREENH6);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.centerY.mas_equalTo(self.backView);
    }];
    
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.centerY.mas_equalTo(self.backView);
        make.width.mas_equalTo(7 * SCREENH6);
        make.height.mas_equalTo(12.5 * SCREENH6);
    }];
    
    [self.billSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.rightImageView);
        make.centerY.mas_equalTo(self.contentView);
    }];
    
    [self.subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backView);
        make.trailing.mas_equalTo(self.rightImageView.mas_leading).offset(-10 * SCREENH6);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.titleLabel);
        make.trailing.mas_equalTo(self.rightImageView);
        make.bottom.mas_equalTo(self.backView);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
}


- (void)switchAction{
    //判断开关的状态
    if (self.billSwitch.on) {
        NSLog(@"switch is on");
        
        NSDictionary * dict = @{@"group_id":self.group_id,@"disturb_status":@"1"};
        
        [[ZRNetworkTool sharedTool] setGroupNodisturbWithDict:dict WithBlock:^(BOOL success, id responseData) {
            
            NSLog(@"res===%@",responseData);
        }];
    } else {
        NSLog(@"switch is off");
        
        NSDictionary * dict = @{@"group_id":self.group_id,@"disturb_status":@"0"};
        
        [[ZRNetworkTool sharedTool] setGroupNodisturbWithDict:dict WithBlock:^(BOOL success, id responseData) {
            
            NSLog(@"res===%@",responseData);
        }];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
